#此模块包含了预约表的增删查改和患者预约
from datetime import date
from typing import Optional
from pydantic import BaseModel
from enum import Enum

class BookTime(str, Enum):
    AM = "AM"
    PM = "PM"

class WhetherDone(str,Enum): 
    Y="Y"
    N="N"

class BookingCreate(BaseModel):
    patient_ID: int
    book_date: date
    book_time: BookTime
    doctor_ID: int
    department: str
    whether_done: WhetherDone = WhetherDone.N  # 默认N

    def check(self):
        if self.patient_ID <= 0:
            raise ValueError("Patient ID must be positive")
        if self.doctor_ID <= 0:
            raise ValueError("Doctor ID must be positive")
        if not 0 < len(self.department) < 50:
            raise ValueError("Department must be between 0 and 50 characters")

class BookingUpdate(BaseModel):
    patient_ID: Optional[int] = None
    book_date: Optional[date] = None
    book_time: Optional[BookTime] = None
    doctor_ID: Optional[int] = None
    department: Optional[str] = None
    whether_done: Optional[WhetherDone] = None

    def check(self):
        if self.doctor_ID is not None and self.doctor_ID <= 0:
            raise ValueError("Doctor ID must be positive")
        if self.department is not None and not 0 < len(self.department) < 50:
            raise ValueError("Department must be between 0 and 50 characters")



#############以下都是患者进行预约的模型
##患者进行预约操作写入预约表模型
class PatientBookingCreate(BaseModel):
    schedule_id: int  # 医生排班ID
    time_period: str  # "上午"或"下午"
    patient_id: int   # 患者ID

    def check(self):
        if self.schedule_id <= 0:
            raise ValueError("Schedule ID must be positive")
        if self.patient_id <= 0:
            raise ValueError("Patient ID must be positive")
        if self.time_period not in ["上午", "下午"]:
            raise ValueError("Time period must be either '上午' or '下午'")

##患者输入自身ID查询所有有关记录
class PatientBookingResponse(BaseModel):
    id: int
    book_date: date
    book_time: str
    doctor_id: int
    doctor_name: str
    doctor_image_url: str
    department: str
    status: str
    ref_ID: int #对应的payment的ID
    whether_done: str

    class Config:
        from_attributes = True

##患者取消预约模型
class CancelBookingResponse(BaseModel):
    message: str


# 病人预约-详情模型
class PatientDetailResponse(BaseModel):
    ID: int
    name: str
    gender: str
    age: int
    phone_number: str

    class Config:
        from_attributes = True



######以下为医生查看自身预约任务模型
class DoctorDailyBookingResponse(BaseModel):
    id: int
    patient_id: int
    book_time: str
    department: str
    status: str
    whether_done: str
    patient_detail: PatientDetailResponse  # 嵌套病人详情

    class Config:
        from_attributes = True